#include "Graphics.hh"

#include <SDL2/SDL_image.h>

using namespace sirtet;
using std::string;

Graphics::Graphics(SDL_Window *window)
{
    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
}

Graphics::~Graphics()
{
    SDL_DestroyRenderer(renderer);
}

SDL_Texture *Graphics::load_png(const string &path)
{
    return IMG_LoadTexture(renderer, path.c_str());
}

void Graphics::clear(Color color)
{
    set_color(color);
    SDL_RenderClear(renderer);
}

void Graphics::draw_texture(SDL_Texture *texture, int x, int y, int width, int height)
{
    SDL_Rect dst{.x = x, .y = y, .w = width, .h = height};
    SDL_RenderCopy(renderer, texture, nullptr, &dst);
}

void Graphics::draw_texture(SDL_Texture *texture, Rect<int> rect)
{
    draw_texture(texture, rect.get_x(), rect.get_y(), rect.get_width(), rect.get_height());
}

void Graphics::fill_rect(Color color, int x, int y, int width, int height)
{
    set_color(color);
    SDL_Rect r{.x = x, .y = y, .w = width, .h = height};
    SDL_RenderFillRect(renderer, &r);
}

void Graphics::fill_rect(Color color, const Rect<int> &rect)
{
    fill_rect(color, rect.get_x(), rect.get_y(), rect.get_width(), rect.get_height());
}

void Graphics::present()
{
    SDL_RenderPresent(renderer);
}

void Graphics::set_color(Color color)
{
    SDL_SetRenderDrawColor(renderer, color.get_red(), color.get_green(), color.get_blue(), color.get_alpha());
}
